import { Component, Vue, Watch } from 'vue-property-decorator';
import { Input, Cascader, AutoComplete, Select } from 'ant-design-vue';
import * as CommonService from '@/service/common-service';
import { CryptUtil } from '@/utils/CryptUtil';

@Component({
  components: {
    'a-input': Input,
    'a-cascader': Cascader,
    'a-auto-complete': AutoComplete,
    'a-select-option': Select.Option
  }
})
export default class Login extends Vue {
  title: string = '登录';
  form: any = {
    username: '',
    password: '',
    unitAreaId: [],
  }
  uuid: string = '';
  options: Array<Object> = [];
  readonly: boolean = false;
  save: boolean = false;
  regionFlag: boolean = false;
  dataSource: Array<Object> = [];

  private async created() {
    //this.title = await CommonService.getSystemConfig({ configKey: 'SYSTEM_LOGIN_TITLE' });
    //let data = await CommonService.getSystemConfig({ configKey: 'IS_LOGIN_REGION_FILTER' })
    //this.regionFlag = this.readonly = +data === 1;
    //this.options = await CommonService.loadAddress({ addressItem: '45' });
  }

  @Watch('form.unitAreaId')
  unitAreaIdChange(val: any[]) {
    this.readonly = !(val.length > 0);
  }

  handleSubmit() {
    let sysConfigMap = {
      "MAPCONFIG_NAME": "mapconfig-guojiatongji.json",
      "MAP_LOCATION_TYPE": "1",
      "SYSTEM_LOGO": "/assets/images/desktop/new-desktop/law_sysTitle.png",
      "IS_LOGIN_REGION_FILTER": "0"
    };
    let loginInfo = { save: this.save, from: this.form };
    let userData: { [string: string]: String } = {};
    userData['username'] = CryptUtil.desEncrypt(this.form.username);
    userData['password'] = CryptUtil.desEncrypt(this.form.password);
    userData['unitAreaId'] = CryptUtil.desEncrypt(JSON.stringify(this.form.unitAreaId));
    debugger
    // 调用登录接口
    CommonService.login(userData).then((data: any) => {
      debugger
      sessionStorage.setItem('token', data['token'] || '');
      let currentHumanInfo = {
        humanId: data.humanId,
        humanName: CryptUtil.desDecrypt(data.humanName)
      };
      sessionStorage.setItem('currentHumanInfo', JSON.stringify(currentHumanInfo));
      //sessionStorage.setItem('sysConfigMap', data['sysConfigMap'] ? data['sysConfigMap']:sysConfigMap.toString());
      sessionStorage.setItem('resources', ''); // 覆盖设置资源信息
      sessionStorage.setItem('loginInfo', CryptUtil.desEncrypt(JSON.stringify(loginInfo)));
      this.$router.push('/desktop');
    })
  }

  async onSearch(value: any) {
    return ; //暂时屏蔽
    let codes = this.form.unitAreaId || [], code = '0';
    if (codes.length === 1) {
      code = codes[0];
    } else if (codes.length === 2) {
      code = codes[1];
    } else {
      code = codes[2];
    }
    let data = await CommonService.searchHumanName({ value, code });
    if (data) {
      data.forEach((e: any) => {
        if (e.unitTypeId === '6' || e.unitTypeId === '7') {
          if (e.humanType === '00') {
            e['humanName'] = e['userName'];
            if (e.subjectName != null) {
              e.userName = `${e.userName}(${e.subjectName})`;
            } else {
              e.userName = e.userName;
            }
          }
        }
      });
      this.dataSource = data;
    } else {
      this.dataSource = [];
    }
  }
}